package com.ruoyi.web.controller.tool;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.IUploadService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@Slf4j
@RestController
public class UploadController {

    @Autowired
    private IUploadService uploadService;
    @Operation(summary = "上传会议室图片")
    @PostMapping("/upload-avatar")
    public AjaxResult uploadAvatar(@RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) {
                throw new RuntimeException("上传的文件为空");
            }
//
//            // 获取用户上下文
//            UserContext userContext = UserContextHolder.getContext();
//            String userId = userContext.getUserid();

            // 调用文件上传服务
            String fileUrl = uploadService.uploadFile(file);
            // 返回成功
            return AjaxResult.success(fileUrl);
        } catch (Exception e) {
            // 捕获异常并返回错误信息
            log.error("文件上传失败: {}", e.getMessage(), e);
            return AjaxResult.error();
        }
    }
}
